<?php
class UserAction extends Action {
	/**
	 * 用户登录
	 */
	public function login()
	{
		$username = $_POST['username'];
		$password = $_POST['password'];

		if(!empty($username) && !empty($password))
		{
			$username = trim($username);
			$user = M('User');
			$condition['username'] = $username;
			$condition['password'] = $password;
			//$type = $user->where($condition)->getField('type');
				
			//$condition['type'] = '1';//管理员1,商家2,app0
			$data = $user->where($condition)->select();
			echo var_dump($data);
			if (empty($data))
			{
				$this->display('Index:login');
			}
			else
			{
				$type = $user->where($condition)->getField('type');
				if($type=='1'){
					$_SESSION["user"] = $data[0];
					//				$this->redirect('/index.php/Index/index2');
					$this->redirect('/User/userListIndex');
				}elseif ($type=='2'){
					$_SESSION["user"] = $data[0];
					$this->redirect('/User/userListSj');
				}
			}
		}
		else
		{
			$this->display('Index:login');
		}
	}

	/**
	 * 首页
	 */
	public function userListIndex()
	{
		if (empty($_SESSION["user"]))
		{
			$this->display('Index:login');
		}else
		{
			$this->display('User:userListIndex');
		}
	}

	/**
	 * 退出
	 */
	public function loguot()
	{
		//$_SESSION["user"]=null;
		//session_destroy();
		unset($_SESSION["user"]);
		$this->redirect('Index/login/');
	}
	/**
	 * 用户列表
	 */
	public function userList()
	{
		$user = M('User');
		if(isset($_POST['name']) && $_POST['name']!=null){
			$where['name'] = array('like',"%{$_POST['name']}%");
		}
		if(isset($_POST['phone']) && $_POST['phone']!=null){
			$where['phone'] = array('eq',"{$_POST['phone']}");
		}
		$where['type'] = array('eq',"0");
		import('ORG.Util.Page');
		$count = $user->where($where)->count();// 查询满足要求的总记录数
		$page = new Page($count,5);
		$limit = $page->firstRow . ',' . $page->listRows;
		$list = $user->where($where)->limit($limit)->order("create_time desc")->select();
		$show = $page->show();
		$this->assign('page',$show);

		$this->assign('list',$list);
		$this->assign('name',$_POST['name']);
		$this->assign('phone',$_POST['phone']);
		$this->display('User:userList');
	}

}